package com.example.vv.model.projectItem.base.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.vv.R;
import com.example.vv.fragmentWork.entity.ProjectListEntity;
import com.example.vv.fragmentWork.entity.WxarticleEntity;
import com.example.vv.model.accountDetails.AIDActivity;
import com.example.vv.model.projectItemDetails.PIDActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class PIAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    List<ProjectListEntity.DatasBean> wxarticleEntities = new ArrayList<>();
    Context context;;
    public PIAdapter(Context context) {

        this.context = context;
    }

    class PIHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.pid_logo)
        ImageView pidLogo;
        @BindView(R.id.pid_content)
        TextView pidContent;
        @BindView(R.id.pid_author)
        TextView pidAuthor;

        public PIHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public void clear(){
        wxarticleEntities.clear();
    }

    public void addData(List<ProjectListEntity.DatasBean> list) {
        wxarticleEntities.addAll(list);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_project_details, parent, false);
        PIHolder holder = new PIHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        PIHolder holder1 = (PIHolder) holder;
        Glide.with(context)
                .load(wxarticleEntities.get(position).getEnvelopePic())
                .into(holder1.pidLogo);
        holder1.pidContent.setText(wxarticleEntities.get(position).getTitle());
        holder1.pidAuthor.setText(wxarticleEntities.get(position).getAuthor());
        holder1.pidLogo.setOnClickListener(v -> {
            EventBus.getDefault().postSticky(wxarticleEntities.get(position));
            context.startActivity(new Intent(context, PIDActivity.class));
        });
    }

    @Override
    public int getItemCount() {
        return wxarticleEntities.size() == 0 ? 0 : wxarticleEntities.size();
    }
}
